package com.hopechart.micro.config;

import org.apache.catalina.connector.Connector;
import org.apache.coyote.http11.Http11NioProtocol;
import org.springframework.boot.context.embedded.EmbeddedServletContainer;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.boot.web.servlet.ServletContextInitializer;

/**
 * <p>
 * 		<br/> 容器配置
 * </p>
 * 
 * @author daijiang@hopechart.com
 * @date 2017-10-30 09:01:13
 * @copyright 杭州鸿泉数字设备有限公司
 * @version V1.0.0
 */
//@Component
//@ConfigurationProperties(prefix = "server", ignoreUnknownFields = true)
public class TomcatConfig extends TomcatEmbeddedServletContainerFactory {
	/**
	 * Server HTTP port.
	 */
	private Integer port = 12306;

	/**
	 * 最大线程数
	 */
	private Integer maxThreads = 1000;

	/**
	 * 最大连接数
	 */
	private Integer maxConnections = 1000;

	/**
	 * 连接超时
	 */
	private Integer connectionTimeout = 30000;

	@Override
	public EmbeddedServletContainer getEmbeddedServletContainer(ServletContextInitializer... initializers) {
		this.setPort(port);
		return super.getEmbeddedServletContainer(initializers);
	}

	@Override
	protected void customizeConnector(Connector connector) {
		super.customizeConnector(connector);
		Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();
		protocol.setMaxThreads(maxThreads);
		protocol.setMaxConnections(maxConnections);
		protocol.setConnectionTimeout(connectionTimeout);
	}

	public void setPort(Integer port) {
		this.port = port;
	}

	public Integer getMaxThreads() {
		return maxThreads;
	}

	public void setMaxThreads(Integer maxThreads) {
		this.maxThreads = maxThreads;
	}

	public Integer getMaxConnections() {
		return maxConnections;
	}

	public void setMaxConnections(Integer maxConnections) {
		this.maxConnections = maxConnections;
	}

	public Integer getConnectionTimeout() {
		return connectionTimeout;
	}

	public void setConnectionTimeout(Integer connectionTimeout) {
		this.connectionTimeout = connectionTimeout;
	}
}
